package org.vehicle.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.vehicle.common.MyConstant;
import org.vehicle.entity.security.MyUserDetail;
import org.vehicle.service.TestService;

@Controller
public class LoginController {
	@Autowired
	private TestService testService;
	/**
	 * 登录
	 * @param req
	 * @return
	 */
	@RequestMapping("login")
	public String login(HttpServletRequest req){
		return "login";
	}
	/**
	 * 登录成功跳转
	 * @param req
	 * @return
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest req, Model model){
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		model.addAttribute("manager", ud);
		model.addAttribute("managerType",MyConstant.MANAGER_TYPE_USER);
		if(ud.getManagerType() == MyConstant.MANAGER_TYPE_USER) {
			return "login";
		}
		req.setAttribute("managerName", ud.getManagerName());
		req.setAttribute("contextPath", req.getContextPath());
		return "index";
	}
	/**
	 * 清空数据（测试）
	 * @param req
	 * @param model
	 */
	@RequestMapping("testTruncateData")
	public String testTruncateData(HttpServletRequest req, Model model){
		testService.truncateData();
		return "redirect:/j_spring_security_logout";
	}
}
